﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using ToolRental.Client.SMvvm.Customer.AdminService;

namespace ToolRental.Client.SMvvm.Customer
{
    public class OrderListServiceAgent : IOrderListServiceAgent
    {
        public async Task<List<Order>> GetDeviceList(int userId, INotifyError notifier)
        {
            List<Order> borrowList = new List<Order>();
            try
            {
                AdminService.OrderListElement[] orderListFromService;
                using (var asc = new AdminServiceClient())
                {
                    orderListFromService = await asc.GetOrderListElementsAsync(userId);
                }
                if (orderListFromService != null)
                {
                    foreach (var borrows in orderListFromService)
                    {
                        borrowList.Add(new Order()
                            {
                                BorrowId = borrows.Id,
                                BrandName = borrows.BrandName,
                                TypeName = borrows.TypeName,
                                Availability = borrows.Availability,
                                Ordered = borrows.Ordered,
                                Borrowed = borrows.Borrowed
                            });
                    }
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található", e);
            }

            return borrowList;
        }



        public async void DoOrder(int toolId, int userId, INotifyError notifier)
        {
            try
            {
                using (var asc = new AdminServiceClient())
                {
                    await asc.NewOrderAsync(toolId, userId);
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található", e);
            }
        }
    }
}
